import java.util.Scanner;

/**
 * Created by IntelliJ
 * Author:熊磊
 */
//低级版本
public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        char key=' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop=true;
        while (loop){
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 取出数据");
            System.out.println("p(peek): 查看队列头数据");
            key =scanner.next().charAt(0); //接受一个字符
            switch (key){
                case 's':
                    try {
                        queue.showQueue();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 'a':
                    try {
                        System.out.println("输入一个数字");
                        int value=scanner.nextInt();
                        queue.add(value);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 'g':
                    try {
                        System.out.println("取出的数据是:"+queue.get());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 'p':
                    try {
                        System.out.println("队列头数据是:"+queue.peek());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop=false;
                    break;
                default:
                    System.out.println("请不要按无关的键");
            }
        }
        System.out.println("程序退出!");

    }
}
class ArrayQueue{
    private int maxSize;  //表示数组最大容量
    private int front;    //队列头
    private int rear;     //队列尾
    private int arr[];    //数据

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front=-1;
        rear=-1;
    }

    public ArrayQueue() {
        maxSize = 10;
        arr = new int[maxSize];
        front=-1;
        rear=-1;
    }

    public boolean isFull(){
        return rear==maxSize-1;
    }

    public boolean isEmpty(){
        return rear==front;
    }

    public void add(int n){
        if (isFull()){
            throw new RuntimeException("队列满了");
        }
        arr[++rear]=n;
    }

    public int get(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[++front];
    }

    public void showQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        else {
            for (int i = 0; i < arr.length; i++) {
                System.out.printf("arr[%d]=%d\n",i,arr[i]);
            }
        }
    }
    //查看头数据
    public int peek(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[front+1];
    }

}